package com.csoa.community.activity;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import com.collect.community.R;
import com.csoa.community.adapter.DataStaticticsAdapter;
import com.csoa.community.bean.DataStatisticsBean;
import com.csoa.community.db.WZMLInfoDB;
import com.csoa.community.db.YFInfoDB;
import com.csoa.community.widget.PullToRefreshListView;

public class DataStatisticsActivity extends BaseActivity {

	private ListView mList;
	private List<DataStatisticsBean> mdatas;
	private DataStaticticsAdapter adapter;
	private EditText etChinesestatistic;
	private EditText etAreaStatistics;
	private Spinner spinnerType;
	private Spinner spinnerSurWay;
	private PullToRefreshListView mPullToRefreshListView;


	@Override
	public void initUI() {
		
		setContentView(R.layout.data_statistics);
		hiddenNar(R.id.nav_query_statistics);
		mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.listview);
		mPullToRefreshListView.setMode(PullToRefreshListView.MODE_NO);
		mList = mPullToRefreshListView.getRefreshableView();
		spinnerType = (Spinner)_activity.findViewById(R.id.spinnerTypeStatistics);
		spinnerSurWay = (Spinner)_activity.findViewById(R.id.spinnerSurway);
		etChinesestatistic = (EditText)this.findViewById(R.id.etChinesestatistic);
		etAreaStatistics = (EditText)this.findViewById(R.id.etAreaStatistics);
		
		
		this.findViewById(R.id.btnRefreshWZML).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						refresh();
					}
				});
		this.findViewById(R.id.btnBackWZMLTable).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
		
		this.findViewById(R.id.btnQueryWZML).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						
						String area = etAreaStatistics.getText().toString().trim();
						String chineseName = etChinesestatistic.getText().toString().trim();
						String type = spinnerType.getSelectedItem().toString();
						String surway = spinnerSurWay.getSelectedItem().toString();
						
						mdatas.clear();
						if(type.equals("全部")) {
							type = "";
						}
						if(surway.equals("全部")) {
							surway = "";
						}
						List<DataStatisticsBean> datas = YFInfoDB.queryStatistics(type, surway, area, chineseName);
						List<DataStatisticsBean> dataMLs = WZMLInfoDB.queryStatistic(type, surway, area, chineseName);
						if ((datas == null || datas.size() == 0) && (dataMLs == null || dataMLs.size() == 0)) {
							DataStatisticsActivity.this.showToast("您搜索的物种信息不存在");
						}else {
							if(datas != null) {
								mdatas.addAll(datas);
							}
							if(dataMLs != null) {
								mdatas.addAll(dataMLs);
							}
						}
						adapter.notifyDataSetChanged();
					}
				});
		
		
	}
	

	@Override
	public void initData() {
		
		String[] strDataType = {"全部","植物"};
		if (strDataType != null) {
			ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
					R.layout.item_spinner, strDataType);
			typeAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerType.setAdapter(typeAdapter);
		}
		
		String[] strDataWay = {"全部","样方法","物种名录"};
		if (strDataWay != null) {
			ArrayAdapter<String> wayAdapter = new ArrayAdapter<String>(this,
					R.layout.item_spinner, strDataWay);
			wayAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerSurWay.setAdapter(wayAdapter);
		}
		
		
		
		mdatas = new ArrayList<DataStatisticsBean>();
		List<DataStatisticsBean> datas = YFInfoDB.queryStatistics();
		List<DataStatisticsBean> dataMLs = WZMLInfoDB.queryStatistic();
		if (datas != null) {
			mdatas.addAll(datas);
		}
		if(dataMLs != null) {
			mdatas.addAll(dataMLs);
		}
		adapter = new DataStaticticsAdapter(this, mdatas);
		mList.setAdapter(adapter);
	}
	
	
	
	public void refresh() {
		List<DataStatisticsBean> datas = YFInfoDB.queryStatistics();
		List<DataStatisticsBean> dataMLs = WZMLInfoDB.queryStatistic();
		mdatas.clear();
		if (datas != null) {
			mdatas.addAll(datas);
		}
		if(dataMLs != null) {
			mdatas.addAll(dataMLs);
		}
		clearEdit();
		adapter.notifyDataSetChanged();
	}
	
	public void clearEdit() {
		etChinesestatistic.setText("");
		etAreaStatistics.setText("");
	}

}
